package com.vess.ratingweb;

import java.util.concurrent.ExecutionException;
import com.vess.ratingweb.model.JDBCAdapter;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginApp extends Activity {

	private TextView mBtnLogin;
	private EditText mEdtUser;
	private EditText mEdtPass;
	private JDBCAdapter mJDBC;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_app);

		mEdtUser = (EditText) findViewById(R.id.ediName);
		mEdtPass = (EditText) findViewById(R.id.ediMail);
		mBtnLogin = (TextView) findViewById(R.id.btnLogin);

		mEdtUser.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				EditText edt = (EditText) v;
				edt.setText("");
				return false;
			}
		});
		
		mEdtPass.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				EditText edt = (EditText) v;
				edt.setText("");
				return false;
			}
		});
		
		mJDBC = new JDBCAdapter(LoginApp.this);
		/*mBtnLogin.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					DrawSomethingView drawView = new DrawSomethingView(LoginApp.this);
					String strPath = drawView.getScreen(mBtnLogin, "abc");
					BitmapDrawable bitmap = new BitmapDrawable(getResources(), strPath);
					mBtnLogin.setBackgroundDrawable(bitmap);
					Log.i("Down", v.getId() + "");
					break;
				case MotionEvent.ACTION_UP:
					mBtnLogin.setBackgroundResource(R.drawable.b_login);
					Log.i("Up", v.getId() + "");
					break;
				default:
					break;
				}
				return false;
			}
		});*/

		mBtnLogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				String[] strLogin = new String[2];
				strLogin[0] = mEdtUser.getText().toString();
				strLogin[1] = mEdtPass.getText().toString();
				if ((strLogin[0].equals("") == true)
						|| (strLogin[1].equals("") == true)) {
					Toast.makeText(LoginApp.this, "Please fill the blank",
							Toast.LENGTH_LONG).show();
				} else {
					loginProcessing lp = new loginProcessing();
					lp.execute(strLogin);
					String strResult = "";
					try {
						strResult = lp.get();
					} catch (InterruptedException e) {
						e.printStackTrace();
					} catch (ExecutionException e) {
						e.printStackTrace();
					}
					if (strResult.equals("-1") == true) {
						Toast.makeText(LoginApp.this,
								"Can't not to connect to Server!",
								Toast.LENGTH_SHORT).show();
					} else {
						if (strResult.equals("0") == true) {
							Toast.makeText(LoginApp.this,
									"Username or password incorrect!!!",
									Toast.LENGTH_SHORT).show();
						} else {
							Toast.makeText(LoginApp.this,
									"Log in successful!!!", Toast.LENGTH_SHORT)
									.show();
							Intent intent = new Intent(LoginApp.this,
									Welcome.class);
							intent.putExtra("id", Integer.parseInt(strResult));
							intent.putExtra("username", strLogin[0]);
							startActivity(intent);
						}
					}
				}
			}
		});
	}

	private class loginProcessing extends AsyncTask<String, String, String> {

		private String strResult;
		private ProgressDialog proDialog;
		
		@Override
		protected String doInBackground(String... params) {
			Log.i("doInBGstrUsername", params[0]);
			Log.i("doInBGstrPassword", params[1]);
			strResult = mJDBC.login(params[0], params[1]);
			Log.i("doInBG", strResult);
			return strResult;
		}

		@Override
		protected void onPostExecute(String result) {
			proDialog.dismiss();
		}

		@Override
		protected void onPreExecute() {
			proDialog = new ProgressDialog(LoginApp.this);
			proDialog.setTitle("Log in");
			proDialog.setMessage("Processing...");
			proDialog.setCanceledOnTouchOutside(false);
			proDialog.show();
		}
	}
}
